<?php
namespace app\admin\controller;

use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;
use app\admin\AdminBase;

/**
 * 权限组
 * Class AuthGroup
 * @package app\admin\controller
 */
class AuthGroup extends AdminBase
{
    protected $auth_group_model;
    protected $auth_rule_model;

    protected function initialize()
    {
        parent::initialize();
        $this->auth_group_model = new AuthGroupModel();
        $this->auth_rule_model  = new AuthRuleModel();
    }

    /**
     * 权限组
     * @return mixed
     */
    public function index($keyword='',$page=1,$limit=15)
    {

        if ($this->request->isAjax()) {


            $where = [];
            if($keyword){
                $where[] = ["title", "like", "%{$keyword}%"];
            }

            $admin_user_list = $this->auth_group_model ->order('id','desc')->paginate($limit);

            return $this->layui_json($admin_user_list->items(),$admin_user_list->total());
        }

        return $this->fetch('index');
    }

    /**
     * 添加权限组
     * @return mixed
     */
    public function create()
    {
        return $this->fetch();
    }

    /**
     * 保存权限组
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->auth_group_model->save($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑权限组
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $auth_group = $this->auth_group_model->find($id);

        return $this->fetch('edit', ['auth_group' => $auth_group]);
    }

    /**
     * 更新权限组
     * @param $id
     */
    public function update($id)
    {

            $data = $this->request->put();

            if ($id == 1 && $data['status'] != 1) {
                $this->error('超级管理组不可禁用');
            }
            if ($this->auth_group_model->where("id",$id)->save($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }

    }

    /**
     * 删除权限组
     * @param $id
     */
    public function delete($id)
    {
        if ($id == 1) {
            $this->error('超级管理组不可删除');
        }
        if ($this->auth_group_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id)
    {
        if ($this->request->isAjax()) {
            $admin_menu_list  = $this->auth_rule_model->
            fieldRaw("id,pid,title,name,status,sort,if(pattern=1,true,false) as isParent")->order('sort' ,'DESC')
                ->order('id' ,'DESC')->select()->toArray();
            $admin_menu_level_list = array2tree($admin_menu_list);
            foreach ($admin_menu_level_list as &$vo){
                if(isset($vo["children"])){
                    $children = $vo["children"];
                    $sort = array_column($children,'sort');
                    array_multisort($sort, SORT_DESC, $children);//排序处理
                    $vo["children"] = $children;
                }

                unset($vo);
            }


            return $this->layui_json($admin_menu_level_list,count($admin_menu_level_list));
        }
        $info = $this->auth_group_model->find($id);
        return $this->fetch('auth', ['info' => $info]);
    }

    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule($id, $auth_rule_ids = '')
    {
        if ($this->request->isPost()) {

            if ($id) {
                $group_data['id']    = $id;
                $group_data['rules'] = $auth_rule_ids;
                $group_auth = $this->auth_group_model->find($id);
                if ($group_auth->save($group_data) !== false) {

                    $this->success('授权成功');
                } else {
                    $this->error('授权失败');
                }
            }
        }
    }
}